﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AllInOne.PrintUtils;
using System.Threading;


namespace AllInOne.Pages
{
    /// <summary>
    /// DemoPrint.xaml 的交互逻辑
    /// </summary>
    public partial class DemoPrintPage : Page
    {
  

        private PrintUtil printUtil = new PrintUtil();

        public DemoPrintPage()
        {
            InitializeComponent();
            //initWindow();

        }
        public void initWindow()
        {
            System.Drawing.Image verifyImage = this.printUtil.GetLoginVerifyCode("10.231.3.1:9091");
            this.showVerifyCode(verifyImage);
        }


        


        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            this.printUtil.Login(this.txtVerifyCode.Text, this.txtYhdh.Text, this.txtMm.Text);
            //var loginFlag = this.printUtil.CheckLogin();
            Thread.Sleep(10000);
            var loginFlag = false;
            var checkLoginIndex = 0;
            while (checkLoginIndex < 50)
            {
                checkLoginIndex++;
                loginFlag = this.printUtil.CheckLogin();
                if (loginFlag)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(200);
                }
            }

            

            MessageBox.Show(loginFlag ? "登陆成功" : "登陆失败");

        }



        private void imgVerify_MouseDown(object sender, MouseButtonEventArgs e)
        {
            System.Drawing.Image verifyImage = this.printUtil.GetLoginVerifyCode("10.231.3.1:9091");
            this.showVerifyCode(verifyImage);

        }



        



        private void btnPrintPaper_Click(object sender, RoutedEventArgs e)
        {
            string zxbh = this.txtZxbh.Text;
            string lsh = this.txtLsh.Text;

            this.StartPrintDrivingLicense(zxbh, lsh);

            //new Thread(new ThreadStart(delegate ()
            //{
            //    this.Dispatcher.Invoke(new Action(delegate ()
            //    {
            //        this.printUtil.PrintPaper(zxbh, lsh);

            //    }));
            //}))
            //{
            //    IsBackground = true
            //}.Start();
        }

        private void showVerifyCode(System.Drawing.Image image)
        {
            var ticks = System.DateTime.Now.Ticks;
            image.Save(string.Format("D:\\image{0}.bmp", ticks), System.Drawing.Imaging.ImageFormat.Bmp);
            this.imgVerify.Source = new BitmapImage(new Uri(string.Format("D:\\image{0}.bmp", ticks)));
        }

        private void btnPrintLogo_Click(object sender, RoutedEventArgs e)
        {
            string lsh = this.txtLsh.Text;
            this.printUtil.PrintLogo(lsh);
        }

        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            this.printUtil.DemoLost("37068419880121734X");
        }



        public System.Drawing.Image GetVerifyCode(string ipAddr)
        {
            System.Drawing.Image verifyImage = this.printUtil.GetLoginVerifyCode(ipAddr);
            return verifyImage;
        }



        public bool loginFlag = false;

        private bool printDrivingLicenseFlag = false;
        private bool printLnmjFlag = false;
        private string lsh = "";
        private string zxbh = "";
        public void InitPrintTask()
        {

            new Thread(new ThreadStart(delegate ()
            {
                while (true)
                {
                    Thread.Sleep(200);
                    if (printDrivingLicenseFlag)
                    {
                        printDrivingLicenseFlag = false;
                        this.Dispatcher.Invoke(new Action(delegate ()
                        {
                            //this.printUtil.PrintPaper(this.zxbh, this.lsh);
                        }));
                    }
                }
            }))
            {
                IsBackground = true
            }.Start();
        }


        public void StartPrintDrivingLicense(string zxbh, string lsh)
        {
            this.zxbh = zxbh;
            this.lsh = lsh;
            //this.printUtil.PrintPaper(this.zxbh, this.lsh);

            //this.printDrivingLicenseFlag = true;
        }


        private System.Timers.Timer checkLoginTimer = null;

        private void initLoginTimer()
        {
            checkLoginTimer = new System.Timers.Timer();
            checkLoginTimer.Interval = 30000;
            checkLoginTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate (object sender, System.Timers.ElapsedEventArgs e)
            {

                if (!loginFlag)
                {
                    return;
                }
                loginFlag = this.printUtil.CheckLogin();
            });
            checkLoginTimer.Enabled = true;
        }

        public bool Login(string verifyCode, string userName, string password)
        {



            SimpleConsole.WriteInfo(string.Format("验证码 {0},username {1},pwd {2}", verifyCode, userName, password), this);
            this.printUtil.Login(verifyCode, userName, password);
            
            Thread.Sleep(1500);
            var checkLoginIndex = 0;
            while (checkLoginIndex < 50)
            {
                checkLoginIndex++;
                loginFlag = this.printUtil.CheckLogin();
                if (loginFlag)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(200);
                }
            }

            return loginFlag;
        }

    }
}
